//
//  FruitLoopsCell.m
//  FruitsWholesaler
//
//  Created by sgone-2 on 16/4/20.
//  Copyright © 2016年 sgone. All rights reserved.
//

#import "FruitLoopsCell.h"
#import "ShareListImageView.h"
#import "FDLineView.h"
#import "UIView+Helper.h"

#define checkNull(__X__)        (__X__) == [NSNull null] || (__X__) == nil ? @"" : [NSString stringWithFormat:@"%@", (__X__)]

@interface FruitLoopsCell () <ShareListImageViewDelegate> {
    
    BOOL isThree;
}

/**
 发布内容视图
 */
@property (nonatomic, strong) UIView *releaseView;
/**
 回复内容视图
 */
@property (nonatomic, strong) UIView *replyView;
/**
 发布内容下划线
 */
@property (nonatomic, strong) FDLineView *releaseLineView;
/**
 回复内容视图下划线
 */
@property (nonatomic, strong) FDLineView *replyLineView;
/**
 发布用户头像
 */
@property (nonatomic, strong) UIImageView *iconImageView;
/**
 发布地址图片
 */
@property (nonatomic, strong) UIImageView *releaseImageView;
/**
 回复图片
 */
@property (nonatomic, strong) UIImageView *replyImageView;
/**
 发布用户名称
 */
@property (nonatomic, strong) UILabel *releaseNameLabel;
/**
 发布时间
 */
@property (nonatomic, strong) UILabel *releaseTimeLabel;
/**
 发布内容
 */
@property (nonatomic, strong) UILabel *releaseContentLabel;
/**
 发布人地址
 */
@property (nonatomic, strong) UILabel *releaseAddressLabel;
/**
 共有N条评论
 */
@property (nonatomic, strong) UILabel *commentCountLabel;
/**
 评论回复数组
 */
@property (nonatomic, strong) NSMutableArray *commentArray;
/**
 *评论图片
 */
@property (nonatomic, strong) ShareListImageView *listImageView;

@end


@implementation FruitLoopsCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
     self.backgroundColor = [UIColor whiteColor];
    
}



- (UIView *)releaseView {
    
    if (_releaseView == nil) {
        
        _releaseView = [[UIView alloc] init];
        [self addSubview:_releaseView];
    }
    return _releaseView;
}

- (FDLineView *)releaseLineView {
    
    if (_releaseLineView == nil) {
        
        _releaseLineView = [[FDLineView alloc] init];
        [self.releaseView addSubview:_releaseLineView];
    }
    return _releaseLineView;
}

- (UIImageView *)iconImageView {
    
    if (_iconImageView == nil) {
        
        _iconImageView = [[UIImageView alloc] init];
        _iconImageView.layer.masksToBounds = YES;
        _iconImageView.layer.cornerRadius = _iconImageView.frame.size.height / 2;
        [self.releaseView addSubview:_iconImageView];
    }
    return _iconImageView;
}

- (UILabel *)releaseNameLabel {
    
    if (_releaseNameLabel == nil) {
        
        _releaseNameLabel = [[UILabel alloc] init];
        _releaseNameLabel.textColor = [UIColor lightGrayColor];
        _releaseNameLabel.font = [UIFont systemFontOfSize:14 / FDLaOt];
        [self.releaseView addSubview:_releaseNameLabel];
    }
    return _releaseNameLabel;
}

- (UILabel *)releaseTimeLabel {
    
    if (_releaseTimeLabel == nil) {
        
        _releaseTimeLabel = [[UILabel alloc] init];
        _releaseTimeLabel.textColor =  [UIColor lightGrayColor];
        _releaseTimeLabel.font = [UIFont systemFontOfSize:12 / FDLaOt];
        [self.releaseView addSubview:_releaseTimeLabel];
    }
    return _releaseTimeLabel;
}

- (UILabel *)releaseContentLabel {
    
    if (_releaseContentLabel == nil) {
        
        _releaseContentLabel = [[UILabel alloc] init];
        _releaseContentLabel.textColor =  [UIColor lightGrayColor];
        _releaseContentLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        _releaseContentLabel.numberOfLines = 0;
        [self.releaseView addSubview:_releaseContentLabel];
    }
    return _releaseContentLabel;
}

- (UILabel *)releaseAddressLabel {
    
    if (_releaseAddressLabel == nil) {
        
        _releaseAddressLabel = [[UILabel alloc] init];
        _releaseAddressLabel.textColor =  [UIColor lightGrayColor];
        _releaseAddressLabel.font = [UIFont systemFontOfSize:12 / FDLaOt];
        [self.releaseView addSubview:_releaseAddressLabel];
    }
    return _releaseAddressLabel;
}

- (UIImageView *)releaseImageView {
    
    if (_releaseImageView == nil) {
        
        _releaseImageView = [[UIImageView alloc] init];
        //        _releaseImageView.backgroundColor = [UIColor cyanColor];
        _releaseImageView.contentMode = UIViewContentModeCenter;
        [self.releaseView addSubview:_releaseImageView];
    }
    return _releaseImageView;
}

- (UIImageView *)replyImageView {
    
    if (_replyImageView == nil) {
        
        _replyImageView = [[UIImageView alloc] init];
        //        _replyImageView.backgroundColor = [UIColor cyanColor];
        _replyImageView.contentMode = UIViewContentModeCenter;
        [self.releaseView addSubview:_replyImageView];
    }
    return _replyImageView;
}

- (UIView *)replyView {
    
    if (_replyView == nil) {
        
        _replyView = [[UIView alloc] init];
        [self addSubview:_replyView];
    }
    return _replyView;
}

- (FDLineView *)replyLineView {
    
    if (_replyLineView == nil) {
        
        _replyLineView = [[FDLineView alloc] init];
        [self.replyView addSubview:_replyLineView];
    }
    return _replyLineView;
}

- (UILabel *)commentCountLabel {
    
    if (_commentCountLabel == nil) {
        
        _commentCountLabel = [[UILabel alloc] init];
        _commentCountLabel.font = [UIFont systemFontOfSize:13 / FDLaOt];
        _commentCountLabel.textColor = FDUSERNAME_COLOR;
        
        
        [self.replyView addSubview:_commentCountLabel];
    }
    return _commentCountLabel;
}

- (NSMutableArray *)commentArray {
    
    if (_commentArray == nil) {
        
        _commentArray = [[NSMutableArray alloc] initWithCapacity:0];
    }
    return _commentArray;
}


-(CGFloat)cellHeight
{
    self.iconImageView.image = [UIImage imageNamed:@"longin_default"];
//    self.releaseNameLabel.text = _messageDic[@"userinfo"][@"wholesaler_name"];
    
    
    self.releaseNameLabel.text = [self getServiceStringAndReturnString:_messageDic[@"userinfo"][@"name"]];
    
    self.releaseTimeLabel.text = [self compareCurrentTime:[NSDate dateWithTimeIntervalSince1970:[_messageDic[@"inputtime"] integerValue]]];
    
    self.releaseContentLabel.text = _messageDic[@"body"];
    self.releaseImageView.image = [UIImage imageNamed:@"dingwei"];
    

    
    NSString *province = [self getServiceStringAndReturnString:_messageDic[@"userinfo"][@"province"]];
    
    NSString *city = [self getServiceStringAndReturnString:_messageDic[@"userinfo"][@"city"]];
    
    NSString *town = [self getServiceStringAndReturnString:_messageDic[@"userinfo"][@"town"]];
    
    if ([province isEqualToString:city]) {
        
        self.releaseAddressLabel.text = [NSString stringWithFormat:@"%@%@",province,town];
    } else
        self.releaseAddressLabel.text = [NSString stringWithFormat:@"%@%@%@",province,city,town];
    
    
    self.replyImageView.image = [UIImage imageNamed:@"reply"];
    
    NSArray *array = [NSArray arrayWithArray:_messageDic[@"friend_discuss"]];
    
    if ([_messageDic[@"discuss_count"] integerValue] <= 3) {
        
        self.commentCountLabel.hidden = YES;
    } else {
        
        isThree = YES;
        self.commentCountLabel.text = [NSString stringWithFormat:@"查看全部%zd条评论",[_messageDic[@"discuss_count"] integerValue]];
    }

    
    for (int i = 0; i < array.count; i ++) {
        
//        UILabel *label = [[UILabel alloc] init];
//        label.text = array[i][@"discuss"];
//        label.textColor = [UIColor lightGrayColor];
//        label.numberOfLines = 0;
//        label.font = [UIFont systemFontOfSize:13 / FDLaOt];
//        
//        [self.replyView addSubview:label];
//        [self.commentArray addObject:label];
        
        
        UILabel *label = [[UILabel alloc] init];
        label.textColor = [UIColor lightGrayColor];
        label.numberOfLines = 0;
        label.font = [UIFont systemFontOfSize:13 / FDLaOt];
        
        NSMutableAttributedString *contentTextStr = nil;
        
        NSString *usernameStr = nil;
        
        if ([checkNull(array[i][@"username"]) length] > 0) {
            
            usernameStr = array[i][@"username"];
        }
        else
        {
            usernameStr = @"";
        }
        
        
        
        
        
        if ([checkNull(array[i][@"pusername"]) length] == 0) {
            
            NSString *fatherStr = [NSString stringWithFormat:@"%@：%@",usernameStr,array[i][@"discuss"]];
            NSString *subStr = usernameStr;
            
       
            contentTextStr = [self getTextByString:fatherStr string:subStr];
        } else {
            
            NSString *firstFatherStr = [NSString stringWithFormat:@"%@ 回复 ",usernameStr];
            NSString *firstSubStr = usernameStr;
            
            contentTextStr = [self getTextByString:firstFatherStr string:firstSubStr];
            
            NSString *secondFatherStr = [NSString stringWithFormat:@"%@：%@",array[i][@"pusername"],array[i][@"discuss"]];
            NSString *secondSubStr = array[i][@"pusername"];
            
            NSMutableAttributedString *contentStr = [self getTextByString:secondFatherStr string:secondSubStr];
            
            [contentTextStr appendAttributedString:contentStr];
        }
        
        label.attributedText = contentTextStr;
        [self.replyView addSubview:label];
        [self.commentArray addObject:label];

    }
    
    
    self.iconImageView.frame = CGRectMake(8, 10, 34 / FDLaOt, 34 / FDLaOt);
    
    self.releaseNameLabel.frame = CGRectMake(self.iconImageView.x + self.iconImageView.width + 8, self.iconImageView.y, ViewWidth - self.iconImageView.width - self.iconImageView.x * 4, 18 / FDLaOt);
    
    self.releaseTimeLabel.frame = CGRectMake(self.iconImageView.x + self.iconImageView.width + 8, self.releaseNameLabel.y + self.releaseNameLabel.height, ViewWidth - self.iconImageView.width - self.iconImageView.x * 4, 16 / FDLaOt);
    
//    CGSize releaseContentSize = [labelAdaptive sizeWithString:self.releaseContentLabel.text font:self.releaseContentLabel.font];
//    
//    //    CGSize textMaxSize = CGSizeMake(self.releaseContentLabel.width, MAXFLOAT);
//    //    NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:14 / FDLaOt]};
//    //    CGFloat textH = [self.releaseContentLabel.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height;
//    
//    if (releaseContentSize.height <= 17 / FDLaOt * contentRow) {
//        
//        self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, releaseContentSize.height);
//    } else {
//        
//        self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, 17 / FDLaOt * contentRow);
//    }
//    if ([_messageDic[@"body"] length] == 0) {
//        
//        self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, 0);
//    }
    
    
    if ([_messageDic[@"body"] length] == 0) {
        
        self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, 0);
    } else {
        
        CGSize releaseContentSize = [labelAdaptive sizeWithString:self.releaseContentLabel.text font:self.releaseContentLabel.font];
        
        //    CGSize textMaxSize = CGSizeMake(self.releaseContentLabel.width, MAXFLOAT);
        //    NSDictionary *textAttrs = @{NSFontAttributeName : [UIFont systemFontOfSize:14 / FDLaOt]};
        //    CGFloat textH = [self.releaseContentLabel.text boundingRectWithSize:textMaxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:textAttrs context:nil].size.height;
        
        if (releaseContentSize.height <= 17 / FDLaOt * contentRow) {
            
            self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, releaseContentSize.height);
        } else {
            
            self.releaseContentLabel.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, ViewWidth - self.iconImageView.x * 2, 17 / FDLaOt * contentRow);
        }
    }

    
    CGSize imagesSize;
    
    if (self.images.count > 0) {
        
        self.listImageView = [[ShareListImageView alloc] init];
        
        self.listImageView.images = self.images;
        self.listImageView.imagesDelegate = self;
        imagesSize = [self.listImageView sizeWithCount:(int)self.images.count];
        
        if ([_messageDic[@"body"] length] == 0) {
            
             self.listImageView.frame = CGRectMake(self.iconImageView.x, self.iconImageView.y + self.iconImageView.height + 8, imagesSize.width, imagesSize.height);
        } else
            
            self.listImageView.frame = CGRectMake(self.iconImageView.x, self.releaseContentLabel.y + self.releaseContentLabel.height + 8, imagesSize.width, imagesSize.height);
        [self.releaseView addSubview:self.listImageView];
        
        self.releaseImageView.frame = CGRectMake(self.iconImageView.x, self.listImageView.y + imagesSize.height + 8, 20 / FDLaOt, 20 / FDLaOt);
    } else {
        
        self.releaseImageView.frame = CGRectMake(self.iconImageView.x, self.releaseContentLabel.y + self.releaseContentLabel.height + 8, 20 / FDLaOt, 20 / FDLaOt);
    }
    
    self.replyImageView.frame = CGRectMake(ViewWidth - 8 - self.releaseImageView.width, self.releaseImageView.y, self.releaseImageView.width, self.releaseImageView.height);
    
    self.releaseAddressLabel.frame = CGRectMake(self.releaseImageView.x + self.releaseImageView.width + 8, self.releaseImageView.y, ViewWidth - self.releaseImageView.width - self.replyImageView.width - 4 * 8, 20 / FDLaOt);
    
    self.releaseView.frame = CGRectMake(0, 0, ViewWidth, self.releaseImageView.y + self.releaseImageView.height + 8);
    
    if (self.commentArray.count > 0) {
        
        self.releaseLineView.frame = CGRectMake(self.iconImageView.x, self.releaseView.height - 0.5, ViewWidth, 0.5);
    }
    
    self.commentCountLabel.frame = CGRectMake(8, 12, ViewWidth - 8 * 2, 20 / FDLaOt);
    
    CGFloat Y;
    if (!isThree) {
        
        Y = 8;
    } else
        
        Y = self.commentCountLabel.y + self.commentCountLabel.height + 8;
    
    isThree = NO;
    
    for (int i = 0; i < self.commentArray.count; i ++) {
        
        UILabel *label = (UILabel *)self.commentArray[i];
        
        CGSize labelSize = [labelAdaptive sizeWithString:label.text font:label.font];
        
        if (labelSize.height <= 17 / FDLaOt * commentRow) {
            
            label.frame = CGRectMake(self.iconImageView.x, Y, ViewWidth - 2 * 8, 17 / FDLaOt);
        } else {
            
            label.frame = CGRectMake(self.iconImageView.x, Y, ViewWidth - 2 * 8, commentRow * 17 / FDLaOt);
        }
        
        Y = Y + label.height + 8;
    }
    
    self.replyView.frame = CGRectMake(0, self.releaseView.height, ViewWidth, Y);
    
    if (self.commentArray.count > 0) {
        
        self.replyLineView.frame = CGRectMake(0, self.replyView.height - 0.5, ViewWidth, 0.5);
        return self.releaseView.height + self.replyView.height;
    }
    
    return self.releaseView.height;

}




- (NSString *)compareCurrentTime:(NSDate *)compareDate {
    
    NSTimeInterval  timeInterval = [compareDate timeIntervalSinceNow];
    timeInterval = -timeInterval;
    long temp = 0;
    NSString *result;
    
    if (timeInterval < 60) {
        result = [NSString stringWithFormat:@"刚刚"];
    }
    else if((temp = timeInterval/60) <60){
        result = [NSString stringWithFormat:@"%ld分前",temp];
    }
    
    else if((temp = temp/60) <24){
        result = [NSString stringWithFormat:@"%ld小时前",temp];
    }
    
    else if((temp = temp/24) <30){
        result = [NSString stringWithFormat:@"%ld天前",temp];
    }
    
    else if((temp = temp/30) <12){
        result = [NSString stringWithFormat:@"%ld月前",temp];
    }
    else{
        temp = temp/12;
        result = [NSString stringWithFormat:@"%ld年前",temp];
    }
    
    return  result;
}






+(instancetype)cellWithTableView:(UITableView *)tableView
{
    
    NSString *ID = @"FruitLoopsCell";
    FruitLoopsCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    if (cell == nil) {
        
        cell = [[NSBundle mainBundle]loadNibNamed:@"FruitLoopsCell" owner:nil options:nil][0];
  
    }
    return cell;
}




- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

}

- (NSMutableAttributedString *)getTextByString:(NSString *)str string:(NSString *)redStr {
    
    NSMutableAttributedString *attrituteString = [[NSMutableAttributedString alloc] initWithString:str];
    
    NSRange range = [str rangeOfString:redStr];
    // 设置标签文字的属性
    [attrituteString setAttributes:@{NSForegroundColorAttributeName: FDUSERNAME_COLOR,   NSFontAttributeName:[UIFont systemFontOfSize:13 / FDLaOt]} range:range];
    
    return attrituteString;
}

- (NSString *)getServiceStringAndReturnString:(NSString *)string {
    
    NSString *returnStr = [checkNull(string) length] > 0 ? string : @"";
    
    return returnStr;
}



- (void)imageWithName:(NSMutableArray *)array andIndex:(int)index {
    
    
    
    [self.delegate imageWithNames:array andIndex:index];
}
@end
